﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using Lon.Server;


namespace Lon.UI
{
    public partial class FormClientView : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        private DevAge.ComponentModel.BoundList<NetNode> mBoundList;

        List<NetNode> displayRecs=new List<NetNode>(100);

     //   List<StationClient> clientList;
        AppServer fs;
        public FormClientView(AppServer fs)
        {
            InitializeComponent();
            this.fs = fs;
            InitList(fs);
            CreateColumns(mBoundList);
        }

        private void InitList(AppServer fs)
        {
          //  clientList = new List<StationClient>();
            displayRecs = fs.NetNodeManager.GetNodeList();

            mBoundList = new DevAge.ComponentModel.BoundList<NetNode>(displayRecs);
            mBoundList.AllowNew = false;
            mBoundList.AllowDelete = false;
            mBoundList.AllowEdit = false;
            
          
        }
        private void CreateColumns(DevAge.ComponentModel.IBoundList boundList)
        {

            //Create columns
            SourceGrid.DataGridColumn gridColumn;
            NetNode sc;
         

            SourceGrid.Cells.Controllers.ToolTipText toolTip=   new SourceGrid.Cells.Controllers.ToolTipText();
            toolTip.ToolTipTitle = "???";
            toolTip.ToolTipIcon = ToolTipIcon.Info;
            toolTip.IsBalloon = true;
            gridColumn = dataGrid.Columns.Add("Name", "站名", typeof(String));
            gridColumn.AutoSizeMode = SourceGrid.AutoSizeMode.EnableStretch | SourceGrid.AutoSizeMode.EnableAutoSize;
           
 
            gridColumn = dataGrid.Columns.Add("Dbm", "电报码", typeof(string));
            gridColumn.AutoSizeMode = SourceGrid.AutoSizeMode.EnableStretch | SourceGrid.AutoSizeMode.EnableAutoSize;
 
            gridColumn = dataGrid.Columns.Add("IP", "IP地址", typeof(string));
            gridColumn.AutoSizeMode = SourceGrid.AutoSizeMode.EnableStretch | SourceGrid.AutoSizeMode.EnableAutoSize;
            gridColumn.DataCell.AddController(toolTip);
               
            gridColumn = dataGrid.Columns.Add("NodeType", "客户端类型", new NodeTypeEditer());
            gridColumn.AutoSizeMode = SourceGrid.AutoSizeMode.EnableStretch | SourceGrid.AutoSizeMode.EnableAutoSize;

            gridColumn = dataGrid.Columns.Add("ConnectedState", "连接状态", new BoolEditer());

            gridColumn.AutoSizeMode = SourceGrid.AutoSizeMode.EnableStretch | SourceGrid.AutoSizeMode.EnableAutoSize;
 

 
            dataGrid.AutoSize = true;
            dataGrid.DataSource = mBoundList;
            dataGrid.AutoSizeCells();
            dataGrid.AutoStretchColumnsToFitWidth = true;
            dataGrid.Columns.StretchToFit();
            
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            InitList(fs);
            this.dataGrid.Invalidate();
        }
    }

  
    /// <summary>
    /// 定义设备属性的输出字符串
    /// </summary>
    class IntListEditor : SourceGrid.Cells.Editors.EditorBase
    {
        public IntListEditor()
            : base(null)
        {
        }
        public override string ValueToDisplayString(object p_Value)
        {
            if (p_Value == null) return "";
            List<int> list = (List<int>)p_Value;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                sb.Append(list[i].ToString());
            }
            return sb.ToString();

        }


    }

    class BoolEditer : SourceGrid.Cells.Editors.EditorBase
    {
        public BoolEditer()
            : base(null)
        {
        }
        public override string ValueToDisplayString(object p_Value)
        {
            if (p_Value == null) return "程序异常";
            if ((bool)p_Value)
            {
                return "正常";
            }
            else
            {
                return "连接断开";
            }

        }


    }
    class NodeTypeEditer : SourceGrid.Cells.Editors.EditorBase
    {
        public NodeTypeEditer()
            : base(typeof(NodeType))
        {
        }
        public static String[] NodeTypeNames = new String[]
        {
            "未定义",
            "站机",
            "终端",
            "应用服务器",
            "通信前置机",
            "维护终端",
            "数据服务器"
        };
        public override string ValueToDisplayString(object p_Value)
        {
            if (p_Value == null) return "";
            int val = (int)p_Value;
            return NodeTypeNames[val];


        }
    }


}
